<?php

class Core {

    // Function to validate the post data
    function validate_post($data)
    {
        // Counter variable
        $counter = 0;

        if(isset($data['hn']) AND !empty($data['hn']) AND
                isset($data['un']) AND !empty($data['un']) AND
                isset($data['db']) AND !empty($data['db'])) {
            return TRUE;
        }

        return FALSE;
    }

    // Function to show an error
    function show_message($type,$message) {
        return $message;
    }

    // Function to write the config file
    function write_config($data) {

        // Config path
        $template_path 	= 'config/database.php';
        $output_path 	= '../system/application/config/database.php';

        // Open the file
        $database_file = file_get_contents($template_path);

        $new  = str_replace("%HOSTNAME%",$data['hn'],$database_file);
        $new  = str_replace("%USERNAME%",$data['un'],$new);
        $new  = str_replace("%PASSWORD%",$data['pw'],$new);
        $new  = str_replace("%DATABASE%",$data['db'],$new);

        // Write the new database.php file
        $handle = fopen($output_path,'w+');

        // Write the file
        fwrite($handle,$new);

        $config_path = '../system/application/config/config.php';
        // Open the file
        $config_file = file_get_contents($config_path);

        $new_config  = str_replace('$config[\'sess_use_database\']            = FALSE;','$config[\'sess_use_database\']            = TRUE;',$config_file);

        // Write the new database.php file
        $handle = fopen($config_path,'w+');

        // Write the file
        fwrite($handle,$new_config);
    }
}